package com.hotel.user.mapper;

import com.hotel.common.entity.Permission;
import com.hotel.common.utils.DBTools;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class PermissionMapper {

    public Permission findById(Long id) {
        String sql = "SELECT id, permission_code, description, created_at FROM permission WHERE id = ?";
        List<Map<String, String>> result = DBTools.query(sql, id);
        if (result.isEmpty()) {
            return null;
        }
        return mapToPermission(result.get(0));
    }

    public Permission findByPermissionCode(String permissionCode) {
        String sql = "SELECT id, permission_code, description, created_at FROM permission WHERE permission_code = ?";
        List<Map<String, String>> result = DBTools.query(sql, permissionCode);
        if (result.isEmpty()) {
            return null;
        }
        return mapToPermission(result.get(0));
    }

    public List<Permission> findAll() {
        String sql = "SELECT id, permission_code, description, created_at FROM permission ORDER BY created_at DESC";
        List<Map<String, String>> result = DBTools.query(sql);
        return result.stream().map(this::mapToPermission).collect(Collectors.toList());
    }

    public List<Permission> findByRoleId(Long roleId) {
        String sql = "SELECT p.id, p.permission_code, p.description, p.created_at " +
                    "FROM permission p INNER JOIN role_permission rp ON p.id = rp.permission_id " +
                    "WHERE rp.role_id = ? ORDER BY p.permission_code";
        List<Map<String, String>> result = DBTools.query(sql, roleId);
        return result.stream().map(this::mapToPermission).collect(Collectors.toList());
    }

    public List<Permission> findByUserId(Long userId) {
        String sql = "SELECT DISTINCT p.id, p.permission_code, p.description, p.created_at " +
                    "FROM permission p INNER JOIN role_permission rp ON p.id = rp.permission_id " +
                    "INNER JOIN user_role ur ON rp.role_id = ur.role_id " +
                    "WHERE ur.user_id = ? ORDER BY p.permission_code";
        List<Map<String, String>> result = DBTools.query(sql, userId);
        return result.stream().map(this::mapToPermission).collect(Collectors.toList());
    }

    private Permission mapToPermission(Map<String, String> map) {
        return Permission.builder()
                .id(Long.valueOf(map.get("id")))
                .permissionCode(map.get("permission_code"))
                .description(map.get("description"))
                .createdAt(LocalDateTime.parse(map.get("created_at").replace(" ", "T")))
                .build();
    }

    public Long insert(Permission permission) {
        String sql = "INSERT INTO permission (permission_code, description, created_at) VALUES (?, ?, ?)";
        LocalDateTime now = LocalDateTime.now();
        DBTools.update(sql, permission.getPermissionCode(), permission.getDescription(), now);
        return Long.valueOf(DBTools.querySingle("SELECT LAST_INSERT_ID()"));
    }

    public int update(Permission permission) {
        String sql = "UPDATE permission SET permission_code = ?, description = ? WHERE id = ?";
        return DBTools.update(sql, permission.getPermissionCode(), permission.getDescription(), permission.getId());
    }

    public int deleteById(Long id) {
        String sql = "DELETE FROM permission WHERE id = ?";
        return DBTools.update(sql, id);
    }

    public int deleteRolePermissionByPermissionId(Long permissionId) {
        String sql = "DELETE FROM role_permission WHERE permission_id = ?";
        return DBTools.update(sql, permissionId);
    }
}